﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Abp.Authorization;
using Abp.Configuration.Startup;
using Abp.Localization;
using GCT.Foundation.Authorization;

namespace GCT.Foundation.CacheState.Authorization
{

    public class CacheStateAuthorizationProvider : AuthorizationProvider
    {
        private readonly bool _isMultiTenancyEnabled;

        /// <summary>
        /// 多租户配置
        /// </summary>
        /// <param name="isMultiTenancyEnabled"></param>
        public CacheStateAuthorizationProvider(bool isMultiTenancyEnabled)
        {
            _isMultiTenancyEnabled = isMultiTenancyEnabled;
        }

        public CacheStateAuthorizationProvider(IMultiTenancyConfig multiTenancyConfig)
        {
            _isMultiTenancyEnabled = multiTenancyConfig.IsEnabled;
        }

        public override void SetPermissions(IPermissionDefinitionContext context)
        {
            var pages = context.GetPermissionOrNull(AppPermissions.Pages) ??
                        context.CreatePermission(AppPermissions.Pages, L("Pages"));
            // 系统管理
            var systemManager = pages.GetOrCreateChildPermission(AppPermissions.Pages_SystemManager, L("SystemManager"));

            // 缓存监控
            var cacheState = systemManager.GetOrCreateChildPermission(CacheStatePermissions.CacheState_Node,
                L("CacheState"));
            cacheState.GetOrCreateChildPermission(CacheStatePermissions.CacheState_Query, L("Query"));
            cacheState.GetOrCreateChildPermission(CacheStatePermissions.CacheState_Delete, L("Delete"));
            cacheState.GetOrCreateChildPermission(CacheStatePermissions.CacheState_FlushDb, L("FlushDb"));
        }

        /// <summary>
        /// 多语言本地化
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, FoundationTemplateConfigs.Localization.SourceName);
        }

    }
}
